#include "PanelLayer.h"
#include "MainWindow.h"



void ColourChangeButton::clicked()
{

	ColourSelectorComp colourSelector (this, canResetToDefault);
	PopupMenu m;
	m.addCustomItem (1234, &colourSelector, 300, 400, false);
	m.showAt (this);
}

void ColourChangeButton::changeListenerCallback (ChangeBroadcaster* source)
{
    ColourSelector* cs = dynamic_cast <ColourSelector*> (source); 
	g_EditorBKColor = cs->getCurrentColour();

	g_pDevice->SetClearColor(g_EditorBKColor.getFloatRed(), g_EditorBKColor.getFloatGreen(), g_EditorBKColor.getFloatBlue());
}

void ColourChangeButton::resetToDefault()
{
	g_EditorBKColor = Colours::grey;

	g_DefaultColor[0] = Colours::blue;
	g_DefaultColor[1] = Colours::grey;
	g_DefaultColor[2] = Colours::red;
	g_DefaultColor[3] = Colours::yellow;
	g_DefaultColor[4] = Colours::aliceblue;
	g_DefaultColor[5] = Colours::antiquewhite;
	g_DefaultColor[6] = Colours::aqua;
	g_DefaultColor[7] = Colours::aquamarine;
	g_DefaultColor[8]  = Colours::azure;
	g_DefaultColor[9]  = Colours::beige;
	g_DefaultColor[10] = Colours::bisque;
	g_DefaultColor[11] = Colours::blanchedalmond;
	g_DefaultColor[12] = Colours::blueviolet;
	g_DefaultColor[13] = Colours::brown;
	g_DefaultColor[14] = Colours::burlywood;
	g_DefaultColor[15] = Colours::cadetblue;
	g_DefaultColor[16]  = Colours::chartreuse;
	g_DefaultColor[17]  = Colours::chocolate;
	g_DefaultColor[18] = Colours::coral;
	g_DefaultColor[19] = Colours::cornflowerblue;
	g_DefaultColor[20] = Colours::cornsilk;
	g_DefaultColor[21] = Colours::crimson;
	g_DefaultColor[22] = Colours::cyan;
	g_DefaultColor[23] = Colours::darkblue;

	g_pDevice->SetClearColor(g_EditorBKColor.getFloatRed(), g_EditorBKColor.getFloatGreen(), g_EditorBKColor.getFloatBlue());
}

///////////////////////////////////////////////////////////////////////////////////
ControlPanelComponent::ControlPanelComponent ()
{
    addAndMakeVisible( m_pChangeBGColorButton = new ColourChangeButton() );

	addAndMakeVisible(m_Texture_Panel = new PropertyPanel());
	
	TextureNameProperty *pName0;
	Array <PropertyComponent*> props0;
    props0.add(pName0 = new TextureNameProperty(0));
	props0.add(new TextureChooseProperty(pName0,0));
	m_Texture_Panel->addSection("Textute0 (Diffuse)", props0);

	Array <PropertyComponent*> props0_1;
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[0]));
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[1]));
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[2]));
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[3]));
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[4]));
	props0_1.add(new SampleStateChooseProperty(0, &g_TextureSampleContex[5]));
	props0_1.add(new ColourPropertyComponent("Border Color", Colour(0x00000000), &g_TexSampleBorderColor[0], true));
 	m_Texture_Panel->addSection("Textute0 SampleState", props0_1);

	TextureNameProperty *pName1;
	Array <PropertyComponent*> props1;
    props1.add(pName1 = new TextureNameProperty(1));
	props1.add(new TextureChooseProperty(pName1,1));
    m_Texture_Panel->addSection("Textute1 (Normal)", props1);

	Array <PropertyComponent*> props1_1;
	m_Texture_Panel->addSection("Textute1 SampleState", props1_1);

	addAndMakeVisible( m_pGroupComponent = new GroupComponent(L"Control Panel", String::empty) );
	m_pGroupComponent->setTextLabelPosition (Justification::centred);
}

ControlPanelComponent::~ControlPanelComponent()
{
}

void ControlPanelComponent::changeListenerCallback(ChangeBroadcaster*)
{
    m_Texture_Panel->refreshAll();
}

void ControlPanelComponent::setTextureSampleStatePropertyComponentEnable(bool bEnable)
{
	m_Texture_Panel->setSectionOpen(1, bEnable);
	m_Texture_Panel->setSectionEnabled(1, bEnable);

	m_Texture_Panel->setSectionOpen(3, bEnable);
	m_Texture_Panel->setSectionEnabled(3, bEnable);
}

void ControlPanelComponent::resized()
{
    m_pGroupComponent->setBounds(4, 0, getWidth()-4, getHeight());
	m_pChangeBGColorButton->setTopLeftPosition( 30, 24 );


    m_Texture_Panel->setBounds( 30, 60, getWidth()-30-4, getHeight() );
}



///////////////////////////////////////////////////////////////////////////////////
MaterialPreviewPage::MaterialPreviewPage(MainComponent *pMainComp)
{
	m_pMainComponent = pMainComp;
    addAndMakeVisible(m_pMaterialInfoComboBox = new ComboBox (L"new combo box"));
    m_pMaterialInfoComboBox->setEditableText(false);
    m_pMaterialInfoComboBox->setJustificationType(Justification::centredLeft);
    m_pMaterialInfoComboBox->setTextWhenNothingSelected(L"opaque_base");
    m_pMaterialInfoComboBox->setTextWhenNoChoicesAvailable(L"(no choices)");
    m_pMaterialInfoComboBox->addItem(L"opaque_base", 1);
    m_pMaterialInfoComboBox->addItem(L"opaque_alphatest", 2);
    m_pMaterialInfoComboBox->addItem(L"transparent", 3);
    m_pMaterialInfoComboBox->addItem(L"line", 4);
    m_pMaterialInfoComboBox->addItem(L"font", 5);
	g_customMaterialID = 5;
	m_pMaterialInfoComboBox->setSelectedId(1);
    m_pMaterialInfoComboBox->addListener(this);

    addAndMakeVisible (m_pMaterialInfoLabel = new Label (L"new label", L"material Name:\n"));
    m_pMaterialInfoLabel->setFont (Font (15.0000f, Font::plain));
    m_pMaterialInfoLabel->setJustificationType (Justification::centredLeft);
    m_pMaterialInfoLabel->setEditable (false, false, false);
    m_pMaterialInfoLabel->setColour (TextEditor::textColourId, Colours::black);
	m_pMaterialInfoLabel->setColour (TextEditor::backgroundColourId, Colour (0x0));
}

MaterialPreviewPage::~MaterialPreviewPage()
{
}

void MaterialPreviewPage::resized()
{
    m_pMaterialInfoComboBox->setBounds (115, 24, 130, 24);
    m_pMaterialInfoLabel->setBounds (5, 24, 150, 24);
}

void MaterialPreviewPage::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
    if(comboBoxThatHasChanged == m_pMaterialInfoComboBox)
    {
		g_DefaultMaterialID = m_pMaterialInfoComboBox->getSelectedItemIndex();
    }
}

void MaterialPreviewPage::visibilityChanged()
{ 
	if( isVisible() )
	{
		g_currentTabName = L"Material Preview";
		m_pMainComponent->setTextureSampleStatePropertyComponentEnable(false);
	}
}

///////////////////////////////////////////////////////////////////////////////////
MaterialCustomPage::MaterialCustomPage(MainComponent *pMainComp)
{
	m_pMainComponent = pMainComp;
	addAndMakeVisible(m_pResetButton = new TextButton(L"ResetToDefault"));
	m_pResetButton->addListener(this);

	addAndMakeVisible(m_Custom_Panel = new PropertyPanel());
	
	Array <PropertyComponent*> props0;
	props0.add(new MaterialBooleanProperty("Lighting", "enabled", "disabled", &g_BooleanLighting));
	props0.add(new MaterialBooleanProperty("depth test", "enabled", "disabled", &g_BooleanDepthTest));
	props0.add(new MaterialBooleanProperty("depth write", "enabled", "disabled", &g_BooleanDepthWrite));
	props0.add(new MaterialBooleanProperty("depth Z greater", "enabled", "disabled", &g_BooleanZFuncGreater));
	props0.add(new MaterialValueFloatProperty("depth bias", &g_DepthBias, -5.0, 5.0, 0.1));
	
	props0.add(new MaterialChooseProperty(&g_CullModeContex));

	props0.add(new MaterialBooleanProperty("Vertex Color", "enabled", "disabled", &g_BooleanVertexColor));
	props0.add(new MaterialBooleanProperty("Specular", "enabled", "disabled", &g_BooleanSpecular));
	props0.add(new MaterialBooleanProperty("Line Mode", "enabled", "disabled", &g_BooleanLineMode));
	
	props0.add(new MaterialChooseProperty(&g_ColorWriteContex));

	props0.add(new MaterialBooleanProperty("Fog", "enabled", "disabled", &g_BooleanFogEnable));

	props0.add(new ColourPropertyComponent("texture factor", Colour(0xFFFFFFFF), &g_TextureFactorColor, true));

    m_Custom_Panel->addSection("RenderState Property", props0);

	Array <PropertyComponent*> props1;
    props1.add(new MaterialBooleanProperty("Alpha Blend", "enabled", "disabled", &g_BooleanAlphaBlend));
	props1.add(new MaterialChooseProperty(&g_AlphaBlendOp));
	props1.add(new MaterialChooseProperty(&g_AlphaBlendSrc));
	props1.add(new MaterialChooseProperty(&g_AlphaBlendDest));
    m_Custom_Panel->addSection("alpha blend", props1);

	Array <PropertyComponent*> props2;
    props2.add(new MaterialBooleanProperty("Alpha Channel Blend", "enabled", "disabled", &g_BooleanSeparateAlphaBlend));
	props2.add(new MaterialChooseProperty(&g_AlphaBlendOpAlpha));
	props2.add(new MaterialChooseProperty(&g_AlphaBlendSrcAlpha));
	props2.add(new MaterialChooseProperty(&g_AlphaBlendDestAlpha));
    m_Custom_Panel->addSection("alpha blend alpha channel", props2);

	Array <PropertyComponent*> props3;
    props3.add(new MaterialBooleanProperty("Alpha Test", "enabled", "disabled", &g_BooleanAlphaTest));
	props3.add(new MaterialChooseProperty(&g_AlphaTestFunc));
	props3.add(new MaterialValueDWORDProperty("Alpha Test Ref", &g_AlphaTestRef, 0, 255, 1.0));
    m_Custom_Panel->addSection("alpha test", props3);

	Array <PropertyComponent*> props4;
	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][0]));
	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][1]));
 	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][2]));
  	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][3]));
  	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][4]));
  	props4.add(new MaterialTextureStageStateChooseProperty(0, &g_TextureStageStateContex[0][5]));
	m_Custom_Panel->addSection("Texture Stage State 0", props4);

	Array <PropertyComponent*> props5;
	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][0]));
	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][1]));
 	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][2]));
  	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][3]));
  	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][4]));
  	props5.add(new MaterialTextureStageStateChooseProperty(1, &g_TextureStageStateContex[1][5]));
	m_Custom_Panel->addSection("Texture Stage State 1", props5);

}

MaterialCustomPage::~MaterialCustomPage()
{
}

void MaterialCustomPage::buttonClicked(Button* buttonThatWasClicked)
{
	if( buttonThatWasClicked == m_pResetButton )
	{
		InitMaterialProperties();
		changeListenerCallback(NULL);
		m_pMainComponent->refreshControlPanel();
	}
}

void MaterialCustomPage::resized()
{
	m_pResetButton->setBounds( getWidth()/3, 4, getWidth()/2, 20 );
	m_Custom_Panel->setBounds( 4, 24, getWidth()-4, getHeight() );
}

void MaterialCustomPage::changeListenerCallback(ChangeBroadcaster*)
{
    m_Custom_Panel->refreshAll();
}

void MaterialCustomPage::visibilityChanged()
{ 
	if( isVisible() )
	{
		g_currentTabName = L"Custom";
		m_pMainComponent->setTextureSampleStatePropertyComponentEnable(true);
	}
}

///////////////////////////////////////////////////////////////////////////////////
SourceCodePage::SourceCodePage(MainComponent *pMainComp)
{
	m_pMainComponent = pMainComp;
	addAndMakeVisible(m_SourceEditor = new TextEditor(L"SourceCode"));
	m_SourceEditor->setMultiLine(true);
	m_SourceEditor->setReadOnly(true);
}

SourceCodePage::~SourceCodePage()
{
}

void SourceCodePage::resized()
{
	m_SourceEditor->setBounds( 4, 4, getWidth()-4, getHeight()-4 );
}


void SourceCodePage::visibilityChanged()
{ 
	if( isVisible() )
	{
		g_currentTabName = L"SourceCode";
		m_pMainComponent->setTextureSampleStatePropertyComponentEnable(false);
		m_SourceEditor->setText(String(g_SourceCodeStr));
	}
}